
#include <iostream>
#include <vector>
#include <memory>

using namespace std;

// 定义 PointerManager 结构体
struct PointerManager {
    std::vector<int*> pointer_container;
};

typedef struct PointerManager PointerManager;

// 清空容器
void clear_container(PointerManager* manager) {
    if (manager) {
        for (int* ptr : manager->pointer_container) {
            cout << "Delete value: " << *ptr << endl;
            std::cout << "Delete address: " << ptr << std::endl;
            delete ptr;
        }
        manager->pointer_container.clear();
    }
}

// 创建 PointerManager 实例
PointerManager* create_pointer_manager() {
    PointerManager* manager = new PointerManager();
    clear_container(manager);
    manager->pointer_container.reserve(10);
    std::cout << "Create pointer container" << std::endl;
    return manager;
}

// 添加指针到容器
void add_ptr(PointerManager* manager, int* ptr) {
    if (manager) {
        manager->pointer_container.push_back(ptr);
    }
}

// 销毁 PointerManager 实例并释放资源
void destroy_pointer_manager(PointerManager* manager) {
    if (manager) {
        clear_container(manager);
        delete manager;
        std::cout << "Close pointer container" << std::endl;
    }
}

// 将 PointerManager 结构体放入 xmlop_data 结构体中
struct xmlop_data {
    PointerManager* manager;
};

typedef struct xmlop_data * xmlop_t;


void *open()
{
    // 预先设置 vector 的容量，避免反复重新分配内存
    // pointer_container.reserve(10); // 假设最多存储 10 个指针
    // pointer_container.clear(); // 清空容器以确保没有旧数据
    // cout << "creat pointer container" << endl;
    struct xmlop_data * data = new xmlop_data();
    data->manager = create_pointer_manager();
    return data;
}


void *close(xmlop_t data)
{
    // for (int* ptr : pointer_container) {
    //     cout << "delete address" << ptr << endl;
    //     delete ptr; // 释放每个指针
    // }
    // pointer_container.clear(); // 清空容器
    // cout << "close pointer container" << endl;
    destroy_pointer_manager(data->manager); // 销毁 PointerManager 实例
    delete data; // 销毁 xmlop_data 实例
    return NULL;
}

void *test1(xmlop_t data)
{
    unique_ptr<int> p(new int(5));
    *p = 10;
    cout << *p << endl;
    int * tmp = p.release();
    cout << "test1 address" << tmp << endl;
    cout << "test1 value" << *tmp << endl;
    // pointer_container.push_back(tmp); // 将指针添加到容器中
    add_ptr(data->manager, tmp);
    return tmp;
}

void *test2(xmlop_t data)
{
    unique_ptr<int> p(new int(5));
    *p = 20;
    cout << *p << endl;
    int * tmp = p.release();
    cout << "test2 address" << tmp << endl;
    cout << "test2 value" << *tmp << endl;
    // pointer_container.push_back(tmp); // 将指针添加到容器中
    add_ptr(data->manager, tmp);
    return tmp;    
}


int main() 
{
    xmlop_t data = (xmlop_t)open();
    void * t1 = test1(data);
    cout << "t1 address" << t1 << endl;
    cout << "t1 value" << *(int *)t1 << endl;

    void * t2 = test2(data);
    cout << "t1 address" << t1 << endl;
    cout << "t1 value" << *(int *)t1 << endl;
    close(data);
    return 0;
}